package com.example.springbootdatamock.mock;

import com.apifan.common.random.RandomSource;
import com.apifan.common.random.source.AreaSource;
import com.apifan.common.random.source.EducationSource;
import com.apifan.common.random.source.OtherSource;
import com.apifan.common.random.source.PersonInfoSource;
import com.example.springbootdatamock.bean.LogData;
import com.example.springbootdatamock.util.HttpUtil;
import com.google.gson.Gson;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Component
@RestController
public class LogMocker implements Runnable {
    @Value("${sleepMs.logSleep}")
    private Integer logSleep;
    @Value("${dest.logUrl}")
    private String logUrl;
    @Value("${dest.param}")
    private String paramName;
    private static final Logger log = LoggerFactory.getLogger(LogMocker.class);

    @RequestMapping("/log")
    public void run() {
        Gson gson = new Gson();
        HttpUtil httpUtil = new HttpUtil(this.logUrl);

        AreaSource areaSource = RandomSource.areaSource();
        EducationSource educationSource = RandomSource.educationSource();
        OtherSource otherSource = RandomSource.otherSource();
        PersonInfoSource personInfoSource = RandomSource.personInfoSource();


        while (true) {

            LogData logData = new LogData(areaSource.randomProvince(), personInfoSource.randomChineseName(), educationSource.randomCollege(), educationSource.randomDegree(), otherSource.randomPlateNumber(RandomUtils.nextBoolean()), personInfoSource.randomChineseNickName(8), personInfoSource.randomQQAccount(), Long.valueOf(System.currentTimeMillis()));

            log.info("模拟日志数据:{}", gson.toJson(logData));
            httpUtil.get(gson.toJson(logData), this.paramName);
            try {
                Thread.sleep(this.logSleep.intValue());

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/app")
    public void app() {
        System.out.println("app");
    }
}



 